package com.ocom.company.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ocom.common.entity.company.EntityDockApi;
import com.ocom.common.vo.company.DockApiListVo;
import com.ocom.common.vo.company.DockApiUrlVo;
import com.ocom.common.vo.company.GetDockApiConfigVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface IDaoDockApi extends BaseMapper<EntityDockApi> {

	@Select({"<script>",
			"select \n" +
					"a.dock_api_code  , a.api_name , u.request_url,a.api_type   \n" +
					"FROM dock_api a \n" +
					"left join dock_url u on u.dock_api_code = a. dock_api_code  and u.grade = #{grade}   \n" +
					" <when test=\"grade!=null and grade == 10  \" > "+
					" and u.server_code = #{serverCode} "+
					" </when> "+
					" <when test=\"grade!=null and grade == 0  \" > "+
					" and u.com_id = #{comId} "+
					" </when> "+
					" where a.dock_code = #{dockCode} and a.api_type in (1,2,3,4)   "+
					" order by dock_api_code "+
					"</script>"})
	List<DockApiUrlVo> getDockApiUrl(@Param("dockCode") String dockCode,
									 @Param("grade") Integer grade,
									 @Param("comId") Long comId,
									 @Param("serverCode") String serverCode);


    /**
     * select 	a.dock_api_code as dockApiCode , a.api_name as apiName , u.request_url as requestUrl
     * from 	dock_api 	a
     * left join 	dock_url 	u 	on 	u.dock_api_code = a. dock_api_code 	and 	u.grade = @grade
     * if(#{@grade } ==10)	and 	u.server_code = serverCode
     * if(#{@grade } ==0)	and 	u.com_id = @comId
     * where 	dock_code = @dockCode 	and 	api_type in (1,3)
     * order by 	dock_api_code
     */
    @Select({"<script>",
            "SELECT\n" +
                    " a.dock_api_code,\n" +
                    " a.api_name,\n" +
                    " a.remark,\n" +
                    " a.api_type,\n" +
                    " a.init_api_para,\n" +
                    " a.page_para,\n" +
                    " a.remark," +
                    " u.request_url\n" +
                    " FROM\n" +
                    " dock_api a\n" +
                    " LEFT JOIN dock_url u ON u.dock_api_code = a.dock_api_code\n" +
                    " AND u.grade = #{grade}\n" +
                    " WHERE\n" +
                    " a.dock_code = #{dockCode}\n" +
                    " <when test=\"dockApiCode!=null \" > " +
                    " AND a.dock_api_code LIKE CONCAT('%',#{dockApiCode},'%')\n" +
                    " </when> " +
                    " <when test=\"apiName!=null  \" > " +
                    " AND a.api_name LIKE CONCAT('%',#{apiName},'%')\n" +
                    " </when> " +
                    " ORDER BY\n" +
                    " a.dock_api_code" +
                    " limit #{page},#{size}" +
                    "</script>"})
    List<DockApiListVo> getDockApiList(@Param("dockCode") String dockCode,
                                       @Param("dockApiCode") String dockApiCode,
                                       @Param("apiName") String apiName,
                                       @Param("grade") Integer grade,
                                       @Param("page") Integer page,
                                       @Param("size") Integer size);

}
